<?php
/**
 * Author Yulin
 * Func   彩种数据获取的方法
 */
defined('IN_YuLin') || exit('NO PERMIT!');

UserLogin();
$res          = array();
$operation    = $_POST['operation'];
$type         = NoBadStr($_POST['type']);
switch ($operation){
    case 'orderList':
        $where        = ' WHERE 1 ';
        // 载入用户的投注记录，最多显示七天
        $orderTab     = Table('lottery_order_main');
        $itemTab      = Table('lottery_order_item');
        $lottTab      = Table('lottery');
        // 如果是代理商查询投注记录
        if ($type){
            if ($type == 'agent_all'){
                $userLineData = array();
                getUserLine($_uid, $userLineData);
                $where .= ' AND main.uid IN (' . implode(',', array_keys($userLineData)) . ')';
            }else{
                // 指定查询某个用户的投注
                $sql         = 'SELECT id FROM ' . Table('user') . ' WHERE username = "' . $type . '"';
                $tagUserInfo = $db->getrow($sql);
                if ($tagUserInfo){
                    $where .= ' AND main.uid = ' . $tagUserInfo['id'];
                }else{
                    $where .= ' AND main.uid = ' . $_uid;
                }
            }
        }else{
            $where .= ' AND main.uid = ' . $_uid;
        }
        
        // 这里原本设计是期号信息按彩种分表，但是不方便查询，后期准备所有的彩种期号都存在这张表中。以tid(lottery表id)关联是哪个彩种的期号
        $perTab       = Table('lottery_k3');
        $sql = 'SELECT main.id, main.lott_id, main.`order_no`, main.`code`, main.`period`, main.`amount`, 
            		main.`prize`, main.`createtime`, main.prize, main.`status`, main.`is_chase`,
                	lott.`ename`, lott.`title` AS lott_title, lott.`ptid`, lott.cate_id, 
                	per.`period`, per.`code` AS kj_code, 
                	item.`cn_play`, item.`en_play`, item.`money`, item.`prize`, item.`winner`, item.betting_no, item.`winner_end`
                FROM ' . $itemTab . ' AS item
                LEFT JOIN ' . $orderTab . ' AS main
                ON item.`o_id` = main.`id`
                LEFT JOIN ' . $lottTab . ' AS lott
                ON main.`lott_id` = lott.`id`
                LEFT JOIN ' . $perTab . ' AS per
                ON item.`period_id` = per.`id`
                ' . $where . ' AND main.createtime >= ' . strtotime(date('Y-m-d', strtotime("-1 week"))) . ' ORDER BY main.createtime DESC';
        $allList        = $db->getall($sql);
        $mainToItem     = array();
        foreach ($allList as $key => $value){
            if ($mainToItem[$value['id']]['id'] == ''){
                $mainToItem[$value['id']]['id']       = $value['id'];
                $mainToItem[$value['id']]['lott_id']  = $value['lott_id'];
                $mainToItem[$value['id']]['order_no'] = $value['order_no'];
                $mainToItem[$value['id']]['code']     = $value['code'];
                $mainToItem[$value['id']]['amount']   = $value['amount'];
                $mainToItem[$value['id']]['createtime']    = $value['createtime'];
                $mainToItem[$value['id']]['re_createtime'] = date('Y-m-d H:i:s', $value['createtime']);
                $mainToItem[$value['id']]['status']        = $value['status'];
                $mainToItem[$value['id']]['is_chase']      = $value['is_chase'];
                $mainToItem[$value['id']]['cate_id']       = $value['cate_id'];
                $mainToItem[$value['id']]['lott_title']    = $value['lott_title'];
            }
            $mainToItem[$value['id']]['prize']        += $value['prize'];
            $mainToItem[$value['id']]['period'][$value['period']][]      = array(
                'ename' => $value['ename'],
                'ptid'       => $value['ptid'],
                'kj_code'    => $value['kj_code'],
                'cn_play'    => $value['cn_play'],
                'en_play'    => $value['en_play'],
                'item_money' => $value['money'],
                'winner'     => $value['winner'],
                'betting_no' => $value['betting_no'],
                'prize'      => $value['prize'],
            );
            if ($mainToItem[$value['id']]['status'] != 2){
                $mainToItem[$value['id']]['status'] = $value['winner'];
            }
        }
        // 获取所有彩种分类信息
        $cateTab      = Table('lottery_category');
        $cateSql      = 'SELECT id, `name`, tags FROM ' . $cateTab;
        $cateList     = $db->getall($cateSql);
        $cateIdKey    = array();
        foreach ($cateList as $k => $v){
            $cateIdKey[$v['id']] = $v;
        }
        // 整理用户的投注订单信息，分为
        $res['list']  = $mainToItem;
        $res['cate']  = $cateIdKey;
        $res['other'] = array(
            'today'     => strtotime(date('Y-m-d')),
            'yesterday' => strtotime(date('Y-m-d', strtotime("-1 day")))
        );
        break;
    case 'orderInfo':
        $oid       = NoBadStr($_POST['oid']);
        if(!$oid){
            $res['status'] = 'error';
            $res['msg']    = '提交数据错误';
            break;
        }
        $orderTab  = Table('lottery_order_main');
        $itemTab   = Table('lottery_order_item');
        $where     = ' WHERE id = ' . $oid;
        $sql       = 'SELECT main.lott_id, main.uid, main.`order_no`, main.`code`, main.`period`, main.`amount`, main.`status`, main.`platform`, main.`prize`, main.`createtime`, 
                        item.`en_play`, item.`cn_play`, item.`betting_no`, item.`num`, item.`money`, item.`odds`, item.`prize`, item.`winner`
                        FROM ' . $orderTab . ' AS main
                        LEFT JOIN ' . $itemTab . ' AS item
                        ON main.`id` = item.`o_id`
                        WHERE main.id = ' . $oid . '
                        ORDER BY main.`lott_id` DESC, item.`betting_no` ASC';
        
        $orderInfo = $db->getall($sql);
        if (!$orderInfo){
            $res['status'] = 'error';
            $res['msg']    = '订单信息未找到';
        } else {
            // 处理订单信息，将查询结果分为订单主信息与从信息
            $order         = array(
                'lott_id'           => $orderInfo[0]['lott_id'],
                'uid'               => $orderInfo[0]['uid'],
                'order_no'          => $orderInfo[0]['order_no'],
                'code'              => $orderInfo[0]['code'],
                'period'            => $orderInfo[0]['period'],
                'amount'            => $orderInfo[0]['amount'],
                'status'            => $orderInfo[0]['status'],
                'platform'          => $orderInfo[0]['platform'],
                'prize'             => $orderInfo[0]['prize'],
                'createtime'        => $orderInfo[0]['createtime'],
                'createtime_format' => date('Y-m-d H:i:s', $orderInfo[0]['createtime']),
            );
            $item          = array();
            foreach ($orderInfo as $k => $v){
                $tmp['en_play']  = $v['en_play'];
                $tmp['cn_play']  = $v['cn_play'];
                $tmp['betting_no'] = $v['betting_no'];
                $tmp['num']      = $v['num'];
                $tmp['money']    = $v['money'];
                $tmp['odds']     = $v['odds'];
                $tmp['prize']    = $v['prize'];
                $tmp['winner']   = $v['winner'];
                $item[]          = $tmp;
            }
            $res['status'] = 'success';
            $res['data']   = array('order' => $order, 'item' => $item);
        }
        break;
    default:
        break;
}
echo json_encode($res);


